<?php
/**
物业端投诉建议
 */
namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\push;


class ComplaintController extends Controller{


    private $app_key = '09ae7f39eb877836195b52df';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='28783d0dacf52cfd12d952a6';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址


    private $appios_key = '1aef5ddc66c9bc4e821e84a3';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='e0a01a1f2e56db54bf06f3fb';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址

    public function AllComplaint(Request $request){
        if($request ->isMethod('post')){
            $village_id      = $request ->input('village_id');
            $data      = DB::table('user')
                ->select('village_id','head','complaints_id','complaints_image','floor','number','room','village_name','complaints_create_time')
                ->join('complaints','complaints.user_complaints','=','user.user_id')
                ->join('village','user.user_village','=','village.village_id')
                ->where('user_village',$village_id)
                ->where('status',0)
                ->orderby('complaints_create_time','ASE')
                ->get();
            foreach ($data as $value){
                // $village_complaints   = $value ->village_complaints;
                $head           = $value ->head;
                $complaints_id  = $value->complaints_id;
                $village_id     = $value ->village_id;
                if(empty($value->complaints_image)){
                    $complaints_image  =[];
                }else{
                    $complaints_image  = explode(',',$value->complaints_image);
                }

                $address           = $value->village_name.'-'.$value->floor.'-'.$value->number.'-'.$value->room;
                $time             = $value->complaints_create_time;
                $da[]             = array(
                    'village_id'   =>$village_id,
                    'complaints_id'=>$complaints_id,
                    'head'          =>$head,
                    'complaints_image'=>$complaints_image,
                    'address'=>$address,
                    'time'=>$time );
            }
            if ($data == true) {
                return json::json('success', '获取报修报事数据成功', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败',[]);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    //获取详情信息
    public function info($complaints_id){
        $data                   = DB::table('complaints')
            ->select('complaints_id','floor','number','room','village_name','complaints_content','complaints_image',
                'status','complaints_create_time','district','head')
            ->leftjoin('user','complaints.user_complaints','=','user.user_id')
            ->leftjoin('village','user.user_village','=','village.village_id')
            ->where('complaints_id',$complaints_id)
            ->get();
        return $data;
    }


    /*    public function reply($complaints_id){
            $data                   = DB::table('complaintsreply')
                ->select('property_head','property_name','complaintsreply_create_time','complaintsreply')
                ->leftjoin('property','complaintsreply.complaintsreply_property','=','property.property_id')
                ->where('complaints_complaintsreply',$complaints_id)
                ->get();
            return $data;
        }*/
    public function reply($complaints_id){
        $data                   = DB::table('complaintsreply')
            ->select('work_head','work_nickname','complaintsreply_create_time','complaintsreply')
            //->leftjoin('village','complaintsreply.complaintsreply_property','=','village.village_id')
            ->leftjoin('work','complaintsreply.complaintsreply_property','=','work.work_id')
            ->where('complaints_complaintsreply',$complaints_id)
            ->get();
        return $data;
    }




    public function SelectComplaint(Request $request){
        if($request ->isMethod('post')){
            $complaints_id          = $request->input('complaints_id');
            $data                   = $this->info($complaints_id);
            foreach ($data as $value){
                $id         = $value->complaints_id;
                $head       = $value ->head;
                $address    = $value ->village_name.$value->floor.$value->number.$value->room;
                $time       = $value->complaints_create_time;
                $complaints_content  = $value->complaints_content;
                if(empty($value->complaints_image)){
                    $complaints_image  =[];
                }else{
                    $complaints_image  = explode(',',$value->complaints_image);
                }
                // $complaints_image    = explode(',',$value->complaints_image);
                $status              = $value ->status;
                $district            = $value ->district;
                $reply               = $this->reply($id);
                foreach ($reply as $key){
                    $da[]     = array('property_head'=>$key->work_head,'property_name'=> $key->work_nickname,
                        'complaintsreply'=>$key->complaintsreply,'time'=>$key->complaintsreply_create_time);
                }
                /* $dd         = array('head'=>$head,'address'=>$address,'time'=>$time,'complaints_content'=>$complaints_content,
                     'complaints_image'=>$complaints_image,'district'=>$district,'status'=>$status);*/
                if(empty($da)){
                    $db    = array('head'=>$head,'address'=>$address,'time'=>$time,'complaints_content'=>$complaints_content,
                        'complaints_image'=>$complaints_image,'district'=>$district,'status'=>$status,'list'=>[]);
                }else{
                    $db    = array('head'=>$head,'address'=>$address,'time'=>$time,'complaints_content'=>$complaints_content,
                        'complaints_image'=>$complaints_image,'district'=>$district,'status'=>$status,'list'=>$da);
                }
            }
            if ($data == true) {
                return json::json('success', '获取报修报事详情成功', $db);
            } else {
                $Array   = (object)array();
                return json::json('failure', '由于网络或者不正当原因失败', $Array);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //物业回复

    public function InsertComplaint(Request $request){
        if($request ->isMethod('post')){
            $Array   = (object)array();
            $user_id              = $request->input('work_id');  //物业ID
            $complaints_id        = $request ->input('complaints_id');  //投诉建议ID
            $content              = $request ->input('content');
            $select               = DB::table('complaintsreply')
                ->where('complaints_complaintsreply',$complaints_id)
                ->where('complaintsreply_property',$user_id)
                ->where('complaintsreply',$content)
                ->get();
            if($select == true){
                return json::json('failure', '你回复的内容已存在', $Array);
            }else{
                $data      = DB::table('complaintsreply')
                    ->insert([
                        'complaints_complaintsreply' =>$complaints_id,
                        'complaintsreply_property'  =>$user_id,
                        'complaintsreply'            => $content
                    ]);
                if($data == true){
                    $content   = "您提的投诉建我们已收到，我们已经在处理中...";
                    $pubs  = DB::table('user')
                           ->leftjoin('complaints','user.user_id','=','complaints.user_complaints')
                           ->where('complaints_id',$complaints_id)
                            ->first();
                    $push   = new push($this ->app_key,$this ->master_secret,$this->url);
                    $push ->send_pub($pubs->phone,$content,'9');
                    $pushios   = new push($this ->appios_key,$this ->masterios_secret,$this->urlios);
                    $pushios ->send_pub($pubs->phone,$content,'9');
                    DB::table('complaints')->where('complaints_id',$complaints_id)->update(['status'=>1]);
                    return json::json('success', '回复成功', $Array);
                }else{
                    return json::json('failure', '由于网络或者不正当原因失败', $Array);
                }
            }
        }
    }

    public function DelComplaint(Request $request){
        if($request ->isMethod('post')) {
            $Array   = (object)array();
            $user_id = $request->input('complaintsreply_id');  //物业ID
            $data      = DB::table('complaintsreply')
                ->where('complaintsreply_id',$user_id)
                ->delete();
            if ($data == true) {
                return json::json('success', '删除成功', $Array);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', $Array);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //投诉建议搜索
    public function SubmitComplaint(Request $request){
        if($request ->isMethod('post')){
            $value           = $request ->input('value');
            $data            =             $data      = DB::table('user')
                ->select('village_id','head','complaints_id','complaints_image','floor','number','room','village_name','complaints_create_time')
                ->join('complaints','complaints.user_complaints','=','user.user_id')
                ->join('village','user.user_village','=','village.village_id')
                ->where('village_name', 'like','%'.$value.'%')
                ->orwhere('floor', 'like','%'.$value.'%')
                ->orwhere('number', 'like','%'.$value.'%')
                ->orwhere('room', 'like','%'.$value.'%')
                ->get();
            foreach ($data as $value){
                // $village_complaints   = $value ->village_complaints;
                $head           = $value ->head;
                $complaints_id  = $value->complaints_id;
                $village_id     = $value ->village_id;
                if(empty($value->complaints_image)){
                    $complaints_image  ="";
                }else{
                    $complaints_image  = explode(',',$value->complaints_image);
                }

                $address           = $value->village_name.$value->floor.$value->number.$value->room;
                $time             = $value->complaints_create_time;
                $da[]             = array(
                    'village_id'   =>$village_id,
                    'complaints_id'=>$complaints_id,
                    'head'          =>$head,
                    'complaints_image'=>$complaints_image,
                    'address'=>$address,
                    'time'=>$time );
            }
            if ($data == true) {
                return json::json('success', '搜索内容获取成功', $da);
            } else {
                return json::json('failure', '您搜索的内容不存',[]);
            }
        }
    }

    public function OkComplaint(Request $request){
        if($request ->isMethod('post')){
            $village_id      = $request ->input('village_id');
            $data      = DB::table('user')
                ->select('village_id','head','complaints_id','complaints_image','floor','number','room','village_name','complaints_create_time')
                ->leftjoin('complaints','complaints.user_complaints','=','user.user_id')
                ->leftjoin('village','user.user_village','=','village.village_id')
                ->where('user_village',$village_id)
                ->where('status',1)
                ->get();
            foreach ($data as $value){
                // $village_complaints   = $value ->village_complaints;
                $head           = $value ->head;
                $complaints_id  = $value->complaints_id;
                $village_id     = $value ->village_id;
                if(empty($value->complaints_image)){
                    $complaints_image  =[];
                }else{
                    $complaints_image  = explode(',',$value->complaints_image);
                }

                $address           = $value->village_name.$value->floor.$value->number.$value->room;
                $time             = $value->complaints_create_time;
                $da[]             = array(
                    'village_id'   =>$village_id,
                    'complaints_id'=>$complaints_id,
                    'head'          =>$head,
                    'complaints_image'=>$complaints_image,
                    'address'=>$address,
                    'time'=>$time );
            }
            if ($data == true) {
                return json::json('success', '获取报修报事数据成功', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败',[]);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }



}